package com.megvii.koala.tv.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.megvii.corelib.components.BaseNetworkErrorView;
import com.megvii.corelib.filechooser.FileChooserActivity;
import com.megvii.corelib.filechooser.FileChooserLayoutParam;
import com.megvii.corelib.util.StorageUtil;
import com.megvii.koala.base.KoalaPresenter;
import com.megvii.koala.tv.R;
import com.megvii.koala.tv.presenter.SettingsPresenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by linyue on 16/1/22.
 */
public class SettingsFragment extends BaseFragment
{
    protected View rootView;

    @Bind(R.id.settings_bluetooth_text)
    protected TextView bluetoothText;

    @Bind(R.id.settings_box_text)
    protected TextView boxText;

    @Bind(R.id.settings_server_text)
    protected TextView serverText;

    @Bind(R.id.settings_recognize_camera_text)
    protected TextView recognizeCameraText;

    @Bind(R.id.settings_display_camera_text)
    protected TextView displayCameraText;

    @Bind(R.id.settings_version_text)
    protected TextView versionText;

    @Bind(R.id.settings_display_local_video_text)
    protected TextView displayLocalVideoText;

    @Bind(R.id.device_id_text)
    protected TextView device_id_text;

    private SettingsListener listener;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState)
    {
        if (rootView == null)
        {
            rootView = inflater.inflate(R.layout.settings_layout, container, false);
        }
        return rootView;
    }

    public void setBluetoothText(String text)
    {
        bluetoothText.setText(text);
    }

    public void setBoxText(String text)
    {
        boxText.setText(text);
    }

    public void setServerText(String text)
    {
        serverText.setText(text);
    }

    public void setRecognizeCameraAmount(int amount)
    {
        recognizeCameraText.setText(String.valueOf(amount));
    }

    public void setDisplayCameraText(String text)
    {
        displayCameraText.setText(text);
    }

    public void setVersionText(String text)
    {
        versionText.setText(text);
    }

    public void setDisplayLocalVideoText(String text){
        displayLocalVideoText.setText(text);
    }

    public void setDeviceIdText(String text){ device_id_text.setText("屏幕编号 ："+text); }

    @OnClick(R.id.settings_bluetooth_container)
    public void onClickBluetooth(View view)
    {
        if (listener != null)
        {
            listener.onClickBluetoothSetting();
        }
    }

    @OnClick(R.id.settings_box_container)
    public void onClickBox(View view)
    {
        if (listener != null)
        {
            listener.onClickBoxSetting();
        }
    }

    @OnClick(R.id.settings_server_container)
    public void onClickServer(View view)
    {
        if (listener != null)
        {
            listener.onClickServerSetting();
        }
    }

    @OnClick(R.id.settings_recognize_camera_container)
    public void onClickRecognizeCamera(View view)
    {
        if (listener != null)
        {
            listener.onClickRecognizeCameraSetting();
        }
    }

    @OnClick(R.id.settings_display_camera_container)
    public void onClickDisplayCamera(View view)
    {
        if (listener != null)
        {
            listener.onClickDisplayCameraSetting();
        }
    }

    @OnClick(R.id.settings_check_update)
    public void onClickCheckUpdate(View view)
    {
        if (listener != null)
        {
            listener.onClickCheckUpdate();
        }
    }

    @OnClick(R.id.settings_display_local_video)
    public void onClickDisplayLocalViedeo(View view){
        if (listener != null)
        {
            listener.onClickLocalFileSetting();
        }
    }

    @Override
    public BaseFragmentListener getBaseFragmentListener()
    {
        return listener;
    }

    @Override
    public Class<? extends KoalaPresenter> getPresenterClass()
    {
        return SettingsPresenter.class;
    }

    @Override
    public BaseNetworkErrorView getNetworkErrorView()
    {
        return null;
    }

    public void setListener(SettingsListener listener)
    {
        this.listener = listener;
    }

    public interface SettingsListener extends BaseFragmentListener
    {
        public void onClickBluetoothSetting();

        public void onClickBoxSetting();

        public void onClickServerSetting();

        public void onClickRecognizeCameraSetting();

        public void onClickDisplayCameraSetting();

        public void onClickCheckUpdate();

        public void onClickLocalFileSetting();
    }

    public void showFileChooser()
    {
        List<String> postfixed = new ArrayList<>();
        postfixed.add(".avi");
        postfixed.add(".mp4");
        postfixed.add(".3gp");
        postfixed.add(".mov");
        String[] rootArray = StorageUtil.getStorageList();
        List<String> roots = new ArrayList<>();
        if (rootArray != null)
        {
            for (String root : rootArray)
            {
                roots.add(root);
            }
        }
        FileChooserLayoutParam param = new FileChooserLayoutParam(R.layout.tv_filechooser_layout,
                R.layout.box_item, R.id.box_item_text, roots, postfixed);
//        for (int i = 0; i < 3; i++)
//        {
            startActivity(FileChooserActivity.class, param);
//        }
    }
}
